(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.fxRate.uploadVerifyController', uploadVerifyController);
	uploadVerifyController.$inject = [ '$scope', '$q', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'fxRateService' ];
	function uploadVerifyController($scope, $q, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, fxRateService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.fxRate = {
			loadId : params.dataKey,
			taskId : params.taskId
		};
		ctrl.fxRateHistoryLookup = function(){
			fxRateService.fxRateHistoryLookup(ctrl.selectedRows);
		};
		ctrl.init = function() {
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.formConfigs.general.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;

			fxRateService.initOptions().then(function(result){
				return $q(function(resolved, rejected) {
					ctrl.currencyOptions = result.currencyOptions;
					ctrl.baseCcy = result.baseCcy;
					resolved();
				});
			}).then(function(){
				ctrl.search();
			});
		};
		
		ctrl.search = function(){
			ctrl.searchCriteria.list = [];
			authService.uploadVerify(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			});
		};
		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		ctrl.uploadVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {		
					'ok' : function(comments) {
						ctrl.fxRate.comments = comments;
						authService.uploadVerifyReject(ctrl.fxRate).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		ctrl.uploadVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.fxRate.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.uploadVerifyApprove(ctrl.fxRate).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.fxrate.list',
					'state' : 'cda.fxRate.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.fxrate.list',
					'state' : 'cda.fxRate.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);